<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//身体状态记录
class Physical extends MY_Controller {

	function __construct(){
		parent::__construct();
		
		$this->load->model('User/UserPhysicalModel', 'item');
	}	
	
	//start
	/**
	* @name 获取身体档案（取最新的记录）
	* @return 
	*/
	//end
	public function get_physical_records(){		
		$user_id = $this->user_id;		
		$res = $this->item->get_new_info($user_id,1);
		if(!$res){
			$this->err_out('没有数据');
		}
		$res = $res['list'];
		//BIM
		if(!empty($res['weight']['value']) && !empty($res['height']['value'])){
			$weight     = $res['weight']['value'];
			$height     = $res['height']['value']/100;		
			$res['BMI']['value'] = sprintf("%.2f",$weight/($height*$height));
		}
		
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//获取状态数据
	public function get_physical(){		
		$user_id = $this->user_id;
		$type_id = $this->input->post('type_id',null,0);
		
		$arr =array(
			"user_id" 	=> $user_id,
			"type_id"	=> $type_id
		);
		
		$list = $this->item->get_lists($arr);
		if(!$list){
			//$this->out(array('message'=>'暂无数据'));
		}
		
		//目标数值
		$this->load->model('User/UserPhysicalTargetModel', 'target');
		$target = $this->target->get_info($user_id,$type_id);
		
		$res = array(
			"list" 	=> $list,
			"target" => $target?$target['value']:''
		);
		
		$data['result_code'] = 0;
		$data['message']     = '成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//获取身体状态项目
	public function get_physical_type(){		
		$this->load->model('User/UserPhysicalTypeModel', 'physicaltype');		
		$res = $this->physicaltype->get_lists();
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	
	//start
	/**
	* @name 添加状态项目记录
	* @param type_id true 项目id
	* @param record true 记录值
	* @return 
	*/
	//end
	public function add_physical(){
		$user_id = $this->user_id;
		
		$type_id = $this->input->post('type_id',null,'0');
		$record  = $this->input->post('record',null,'');
		
		$time = time();
		$date = date("Y-m-d",$time);
		
		if(!$type_id){
			$this->err_out('请选择类型');
		}
		
		$info = $this->item->date_info($user_id,$type_id,$date);
		if($info){
			$arr = array(								
				"record"	=> $record,				
				"addtime"	=> $time
			);		
			$res = $this->item->edit($arr," user_id='{$user_id}' and type_id='{$type_id}' and date='{$date}' ");
		}else{

			$ls_res = $this->item->get_new_info($user_id);
			$old_data = $ls_res['list'];
			foreach($old_data as $val){
				
				$arr = array(
					"user_id" 	=> $user_id,
					"type_id"	=> $val['type_id'],
					"date"		=> $date,	
					"addtime"	=> $time
				);
				
				if($val['type_id']==$type_id){
					$arr['record'] = $record;
				}
				else{
					$arr['record'] = $val['value'];
				}
				
				$res = $this->item->add($arr);
			}

		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'添加成功':'添加失败';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//添加目标
	public function add_target(){
		$user_id = $this->user_id;
		$type_id	 = $this->input->post('type_id',null,'0');
		$value  = $this->input->post('value',null,'');
		
		if(!$type_id){
			$this->err_out('请选择类型');
		}
		
		$this->load->model('User/UserPhysicalTargetModel', 'target');
		$info = $this->target->get_info($user_id,$type_id);
		if($info){
			$arr = array(								
				"value"	=> $value				
			);		
			$res = $this->target->edit($arr," user_id='{$user_id}' and type_id='{$type_id}' ");
		}else{
			$arr = array(
				"user_id" 	=> $user_id,
				"type_id"	=> $type_id,
				"value"		=> $value				
			);		
			$res = $this->target->add($arr);
		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'添加成功':'添加失败';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
}
